package fr.umlv.m2.uwp2011.service.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BouchonUserDAO implements UserDAO{
	private static final Map<String, UserDTO> mapUser = new HashMap<String, UserDTO>();
	private static final Map<String, String> mapPassword = new HashMap<String,String>();

	static {
		String login;
		String password;
		//--------- user etud1 -----------//
		login = "etud1";
		password = new String(login);
		createStudent(login, password, "olivier", "chantefoin");

		
		//--------- user etud2 -----------//
		login = "etud2";
		password = new String(login);
		createStudent(login, password, "jenseric", "catbagan");		

		
		//--------- user etud3 -----------//
		login = "etud3";
		password = new String(login);
		createStudent(login, password, "viravout", "alexandre");

		
		//--------- user etud4 -----------//
		login = "etud4";
		password = new String(login);
		createStudent(login, password, "william", "choy");
	
		//--------- user etud5 -----------//
		login = "etud5";
		password = new String(login);
		createStudent(login, password, "bertrand", "gambouele");
		
		//--------- user etud6 -----------//
		login = "etud6";
		password = new String(login);
		createStudent(login, password, "patrick", "vongpraseuth");
		
		//--------- user etud7 -----------//
		login = "etud7";
		password = new String(login);
		createStudent(login, password, "noam", "yoro");
		
		//--------- user prof1 -----------//
		login = "prof1";
		password = new String(login);
		createTeacher(login, password, "nicolas", "bedons");
		
		//--------- user prof2 -----------//
		login = "prof2";
		password = new String(login);
		createTeacher(login, password, "gauthier", "loyaute");
		
		//--------- user prof3 -----------//
		login = "prof3";
		password = new String(login);
		createTeacher(login, password, "chloe", "rispal");
		
		//--------- user prof4 -----------//
		login = "prof4";
		password = new String(login);
		createTeacher(login, password, "hakim", "badis");
		
		//--------- user secre1 -----------//
		login = "secre1";
		password = new String(login);
		createSecretary(login, password, "nathalie", "lepoittevin");
		
		//--------- user secre2 -----------//
		login = "secre2";
		password = new String(login);
		createSecretary(login, password, "nicole", "pagnini");
		
		//--------- user secre3 -----------//
		login = "secre3";
		password = new String(login);
		createSecretary(login, password, "nathalie", "rouquette");
		
	}
	
	private static void createPeople(UserDTO userDTO, String login, String password, String givenName, String lastName) {
		userDTO.setGivenName(givenName);
		userDTO.setLastName(lastName);
		userDTO.setLogin(login);
		userDTO.setMail(login+"@univ-mlv.fr");
		userDTO.setPassword(password);
		mapPassword.put(login, password);
		mapUser.put(login, userDTO);
	}
	
	
	private static void createStudent(String login, String password, String givenName, String lastName) {
		UserDTO userDTO = new UserDTO();
		createPeople(userDTO, login, password, givenName, lastName);
		userDTO.setGroup(UserDTO.STUDENT);
	}
	
	private static void createTeacher(String login, String password, String givenName, String lastName) {
		UserDTO userDTO = new UserDTO();
		createPeople(userDTO, login, password, givenName, lastName);
		userDTO.setGroup(UserDTO.TEACHER);
	}
	
	private static void createSecretary(String login, String password, String givenName, String lastName) {
		UserDTO userDTO = new UserDTO();
		createPeople(userDTO, login, password, givenName, lastName);
		userDTO.setGroup(UserDTO.SECRETARY);
	}


	@Override
	public UserDTO getUserDetails(String login, String password) {
		if(mapPassword.containsKey(login)) {
			String pass= mapPassword.get(login);
			if(pass!=null && pass.equals(password)) {
				return mapUser.get(login);
			}
		}
		throw new IllegalArgumentException("Wrong login or wrong password");
	}

	@Override
	public UserDTO getUserDetails(String login) {
		return mapUser.get(login);
	}

	@Override
	public boolean insertUser(UserDTO userDTO) {
		return mapUser.put(userDTO.getLogin(), userDTO) != null;
	}

	@Override
	public boolean updateUser(UserDTO userDTO) {
		if(!mapUser.containsKey(userDTO.getLogin())) {
			return false;
		}
		return mapUser.put(userDTO.getLogin(), userDTO) != null;
	}

	@Override
	public boolean deleteUser(UserDTO userDTO) {
		if(!mapUser.containsKey(userDTO.getLogin())) {
			return false;
		}
		return mapUser.remove(userDTO.getLogin()) != null;
	}


	@Override
	public List<UserDTO> getAllUser() {
		List<UserDTO> list = new ArrayList<UserDTO>(mapUser.values().size());
		for(UserDTO user :  mapUser.values()) {
			list.add(user);
		}
		return list;
	}


	@Override
	public List<UserDTO> getGroupUser(int group) {
		List<UserDTO> list = new ArrayList<UserDTO>(mapUser.values().size());
		for(UserDTO user :  mapUser.values()) {
			if(user.getGroup() == group) {
				list.add(user);
			}
		}
		return list;
	}

}
